package com.hn658.groupshare.core.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hn658.framework.common.AbstractBusinessObject;
import com.hn658.framework.shared.utils.UUIDUtils;
import com.hn658.groupshare.common.entity.UserInfoEO;
import com.hn658.groupshare.core.dao.redis.IUserInfoRedisDAO;
import com.hn658.groupshare.core.service.ISessionService;
import com.hn658.groupshare.core.service.IUserInfoService;

@Component
public class SessionServiceImpl extends AbstractBusinessObject implements ISessionService {
	
	@Autowired
	IUserInfoRedisDAO userInfoRedisDAO;
	
	@Autowired
	IUserInfoService userInfoService;
	
	@Override
	public String createSession(Long uid) {
		// 生成新的authkey
		String authkey = UUIDUtils.getUUID();
		// 保存authkey
		userInfoRedisDAO.setUserAuthkey(uid, authkey);
		return authkey;
	}

	@Override
	public String getAuthkey(Long uid) {
		return userInfoRedisDAO.getAuthkeyByUid(uid);
	}

	@Override
	public UserInfoEO getUserByAuthkey(String authkey) {
		Long uid = userInfoRedisDAO.getUidByAuthkey(authkey);
		if(uid==null){
			return null;
		}
		return userInfoService.findUserByUid(uid);
	}

	@Override
	public void removeSession(Long uid) {
		userInfoRedisDAO.removeUserAuthkey(uid);
	}
}
